package com.cdtu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdtu.dto.BuyTicketParam;
import com.cdtu.dto.R;
import com.cdtu.dto.TicketDTO;
import com.cdtu.entity.Order;
import com.cdtu.entity.Sights;
import com.cdtu.entity.Ticket;
import com.cdtu.service.OrderService;
import com.cdtu.service.SightsService;
import com.cdtu.service.TicketService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : Sakura
 * @Date : 2023/10/28 18:52
 * @Description : TODO 用来接收...的请求
 */
@RestController
@RequestMapping("/api/ticket")
//解决跨域问题
@CrossOrigin
public class TicketController {

    @Autowired
    private TicketService ticketService;

    @Autowired
    private SightsService sightsService;

    @Autowired
    private OrderService orderService;
    @GetMapping("/list/{pageNo}/{pageSize}")
    public R list(@PathVariable Integer pageNo,
                  @PathVariable Integer pageSize,
                  @RequestParam(required = false) String sightId) {

        LambdaQueryWrapper<Ticket> sightWrapper = new LambdaQueryWrapper<>();
        sightWrapper.eq(sightId!=null&&sightId!="",Ticket::getSightId, sightId);
//        这里的search应该是景点的searchKey
        Page<Ticket> page = ticketService.page(new Page<>(pageNo, pageSize), sightWrapper);
        Page<TicketDTO> pageDTO = new Page<>(pageNo, pageSize);
        BeanUtils.copyProperties(page, pageDTO, "records");

        List<TicketDTO> dtoList = page.getRecords().stream().map(item -> {
            TicketDTO dto = new TicketDTO();
            BeanUtils.copyProperties(item, dto);
            Sights sights = sightsService.getById(item.getSightId());
            if (sights.getName() != null) {
                dto.setSightName(sights.getName());
            }
            return dto;
        }).collect(Collectors.toList());
        pageDTO.setRecords(dtoList);
        return R.success(pageDTO);
    }

    @GetMapping("/detail/{tid}")
    public R ticketDetail(@PathVariable("tid") long tid) {
        Ticket ticket = ticketService.getById(tid);
        return R.success(ticket);
    }

    @DeleteMapping("/delete/{tid}")
    public R deleteticket(@PathVariable("tid") long tid) {
        boolean remove = ticketService.removeById(tid);
        if (!remove) {
            return R.error("删除失败，请重新尝试");
        }
        return R.success("删除成功");
    }

    @PostMapping("addOrUpdate")
    public R saveOrUpdate(@RequestBody Ticket ticket) {

        //id为0则是添加
        if (ticket.getId() == 0) {
            boolean save = ticketService.save(ticket);
            if (!save) {
                return R.error("添加失败");
            }
            return R.success("添加成功");
        }
        //修改
        boolean update = ticketService.updateById(ticket);
        if (!update) {
            return R.error("修改失败");
        }
        return R.success("修改成功");
    }


    @GetMapping("/getBySid/{sid}")
    public R getBySid(@PathVariable("sid") long sid) {
        LambdaQueryWrapper<Ticket> ticketWrapper = new LambdaQueryWrapper<>();
        ticketWrapper.eq(Ticket::getSightId, sid);
        List<Ticket> list = ticketService.list(ticketWrapper);
        return R.success(list);
    }

    @PostMapping("/buy")
    public R buy(@RequestBody BuyTicketParam ticketParam) {
        Order order = new Order();
        order.setBuyTime(new Date());
        order.setPhone(ticketParam.getPhone());
        order.setUserId(ticketParam.getUserId());
        order.setTicketId(ticketParam.getTicketId());
        boolean save = orderService.save(order);
        if (!save) {
            return R.error("购买失败,请重新尝试");
        }
        return R.success("购买成功");
    }
}
